<?php 
/**
 * 缓存类
 * 支持文件缓存，内相聚缓存,cookie缓存和数据库缓存
 *依赖类库Memcache,dataBase
 *@author:jackbrown
 *@version:1.0.2;
示例
require('global.php');

$c = new cache('test','array_file');

$stime = run_time();
if($c->isCached()){
	echo 'has cache--';
	$data = $c->readCache();
	$etime = run_time();
}else{
	$c1 = new cache('product','database');

	$data = $con->getResultArray('select*from happy_product_infor limit 3000');
	$etime = run_time();
	$c->writeCache($data);
}


echo $etime-$stime;

//print_r($data);

$c->cacheType = 'html_file';
$c->cacheKey = 'test_html';
//$c->clearCache();
echo $c->readCache();
echo '<br />over-----------------------------------<br />';


$c1->writeCache('select prod_name,prod_pic from test.happy_product_infor limit 6');
print_r($c1->readCache());
*/

class cache{

	public $cacheType = 'html_file';
	public $cacheKey = '';
	public $expires = 30;
	
	private $mem;
	private $dbCon;
	
	public function __construct($key='',$type=''){
		
		if(!file_exists(CACHE_DIR)){
		
			exit('缓存目录不存在!');
		}
		
		if($key!=''){
			$this->cacheKey = $key;
		}
		
		if(in_array($type,array('html_file','array_file','memory','database'))){
			$this->cacheType = $type;
		}
		
		if($this->cacheType == 'memory'){
		
			if(!class_exists('Memcache')){
			
				die('内存缓存库没有安装');
			}
			
			$this->mem = new Memcache();
			if(!$this->mem->connect('localhost',11211)){
				die('memcache连接失败!');
			}
		}else if($this->cacheType == 'database'){
			
			/*切换数据库为cache数据库*/
			$this->dbCon = new dataBase();
			
		}

	}
	
	
	public function isCached(){
	
		if($this->cacheKey == ''){
			
			return false;
		}
		if($this->cacheType == 'html_file' || $this->cacheType == 'array_file'){
		
			if(file_exists(CACHE_DIR.$this->cacheKey.'.cache')){

				return (strtotime(date('Y-m-d H:i:s'))-filemtime(CACHE_DIR.$this->cacheKey.'.cache')<=$this->expires);
			}
			
			return false;
		}
		
		if($this->cacheType == 'database'){
			
			return $this->dbCon->existsTable($this->cacheKey);	
		}
		
		return false;
	}
	
	public function readCache(){
		if($this->cacheKey == ''){
			
			return false;
		}
		

		
		if($this->cacheType == 'html_file'){
			
			return @file_get_contents(CACHE_DIR.$this->cacheKey.'.cache');
			
		}else if($this->cacheType == 'array_file'){
		
			return unserialize(@file_get_contents(CACHE_DIR.$this->cacheKey.'.cache'));
			
		}else if($this->cacheType == 'memory'){
			
			return $this->mem->get($this->cacheKey,0,$this->expires);
			
		}else if($this->cacheType == 'database'){
			
			$data = array();
			$this->dbCon->useDataBase('cache');
			$data['data'] = $this->dbCon->getResArray("select*from ".$this->cacheKey);
			$resum = $this->dbCon->getResArray("select resum from recordsum where cachekey = '".$this->cacheKey."' limit 1",true);
			$data['sum'] = $resum['resum'];
			
			$this->dbCon->useDataBase(DBNAME);
			return $data;
		}	
	}
	
	
	/*
	写入缓存数据
	@param string/array 数据内容，可为数组或sql查询语句
	@param int $sum 数据总数，用于database型的缓存
	*/
	public function writeCache($data=null,$sum=null){
		if($this->cacheKey == ''){
			
			return false;
		}
	
		if($this->cacheType == 'html_file'){
		
			if($data == null){
				$data = ob_get_contents();
				ob_flush();
			}
			
			file_put_contents(CACHE_DIR.$this->cacheKey.'.cache',$data,LOCK_EX);
			
		}else if($this->cacheType == 'array_file'){
			
			if($data == null){
				
				return false;
			}
			
			$data = serialize($data);
			file_put_contents(CACHE_DIR.$this->cacheKey.'.cache',$data,LOCK_EX);
			
		}else if($this->cacheType == 'memory'){
			
			if($data == null){
				return false;
			}
			
			$this->mem->set($this->cacheKey,$data);
		
		}else if($this->cacheType == 'database'){
			
			if($data == null){
				return false;
			}
			
			$this->dbCon->useDataBase('cache');
			
			/*记录总数*/
			$insert = array();
			if(is_numeric($sum) && $sum>=0){
				$insert['resum'] = $sum;
			}else{
				$insert['resum'] = $this->dbCon->recordCount($data);
			}

			if($this->dbCon->existsTable($this->cacheKey)){
				
				$this->dbCon->query('drop TABLE '.$this->cacheKey);
				$this->dbCon->update('recordsum',$insert,"cachekey = '".$this->cacheKey."'");
									 
			}else{
				$insert['cachekey'] = $this->cacheKey;
				$this->dbCon->insert('recordsum',$insert);
			}
			
			return $this->dbCon->copyFromSQL($this->cacheKey,$data) && $this->dbCon->useDataBase(DBNAME);
		}
	
	}
	
	/*
	清除缓存
	*/
	public function clearCache($key=''){
	
		if($key!=''){
			$this->cacheKey = $key;	
		}
		
		if($this->cacheKey == ''){
			
			return false;
		}
	
		if($this->cacheType == 'html_file' || $this->cacheType == 'array_file'){
		
			return @unlink(CACHE_DIR.$this->cacheKey.'.cache');
		}
		
		if($this->cacheType == 'memory'){
			
			if($key == ''){
				return $this->mem->flush();	
			}
			return $this->mem->delete($this->cacheKey);
		}
		
		if($this->cacheType == 'database'){
			
			return $this->dbCon->query('drop table '.$this->cacheKey);	
		}
	}
	

	
	function __destruct(){
		
		@$this->mem->close();

	}
}


?>